package org.zzz.controller;

import org.zzz.entity.*;
import org.zzz.entity.dto.RegisterDto;
import org.zzz.service.RoleService;
import org.zzz.service.StudentService;
import org.zzz.service.TeacherService;
import org.zzz.service.UserService;
import org.zzz.service.impl.RoleServiceImpl;
import org.zzz.service.impl.StudentServiceImpl;
import org.zzz.service.impl.TeacherServiceImpl;
import org.zzz.service.impl.UserServiceImpl;

import org.zzz.utils.Result;

/**
 * @Author ZZF
 * @Time 2021/04/20 21:15
 */
public class UserController {
    private UserService userService = new UserServiceImpl();;
    private RoleService roleService = new RoleServiceImpl();
    private TeacherService teacherService = new TeacherServiceImpl();
    private StudentService studentService = new StudentServiceImpl();


//    public Result<User> getUser(){
//        return Result.success("查询成功").data(userService.getUser());
//    }

    /**
     * 登录
     * @param username
     * @param password
     * @return
     */
    public Result<String> login(String username,String password){
        User loginUser = userService.login(username, password);
        if(loginUser==null){
            return Result.error("账号和密码错误!");
        }
        String roleName = roleService.getRoleNameById(loginUser.getRoleId());
        return Result.success("登录成功!").data(roleName);
    }

    /**
     * 注册,同时录入学生信息/教师信息,classId为null录入教师信息，degree为null录入学生信息，不能两个都为null
     * @param registerDto
     * @param classId
     * @param degree
     * @return
     */
    public Result register(RegisterDto registerDto,String classId,String degree){
        if((classId == null && degree == null )|| (classId != null && degree != null)){
            return Result.error("注册失败");
        }else{
            if(userService.getUserByUsername(registerDto.getUsername())!=null){
                return Result.error("用户名存在");
            }
            if (registerDto == null){
                return Result.error("信息不能为空");
            }else{
                userService.insertUser(new User(registerDto.getUsername(), "123456", registerDto.getRoleId()));
                if (classId != null) {
                    studentService.insertStudent(new Student(registerDto.getUsername(), registerDto.getName(), registerDto.getGender(), classId));
                    return Result.success("注册成功");
                }
                if (degree != null) {
                    teacherService.insertTeacher(new Teacher(registerDto.getUsername(), registerDto.getName(), registerDto.getGender(), degree));
                    return Result.success("注册成功");
                }
                return Result.error("注册失败");
            }
        }
    }

    /**
     * 根据学号获取学生信息
     * @param id
     * @return
     */
    public Result<StudentVo> getStudentById(String id){
        StudentVo studentVo = userService.getStudentById(id);
        return studentVo != null ? Result.success("查询成功").data(studentVo) : Result.error("查无此学生");
    }

    /**
     * 根据教师工号获取教师信息
     * @param id
     * @return
     */
    public Result<TeacherVo> getTeacherById(String id){
        TeacherVo teacherVo = userService.getTeacherById(id);
        return teacherVo != null ? Result.success("查询成功").data(teacherVo) : Result.error("查无此教师");
    }

    /**
     * 根据用户名删除学生/教师信息 flag 为 1 删除学生信息，flag为2 删除教师信息
     * @param username
     * @param flag
     * @return
     */
    public Result deleteUser(String username,int flag){
        return userService.deleteUser(username,flag) == true ? Result.success("删除成功") : Result.error("删除失败");
    }


    /**
     * 修改教师信息
     * @param teacherVo
     * @return
     */
    public Result updateTeacher(TeacherVo teacherVo){
        return userService.updateTeacher(teacherVo) == true ? Result.success("修改成功") : Result.error("修改失败");
    }

    /**
     * 修改学生信息
     * @param studentVo
     * @return
     */
    public Result updateStudent(StudentVo studentVo){
        return userService.updateStudent(studentVo) == true ? Result.success("修改成功") : Result.error("修改失败");
    }

    public Result updateUserById(String sourceId,String targetId){
        return userService.updateUserById(sourceId, targetId) == true ? Result.success("修改成功") :Result.error("修改失败");
    }





}
